package com.icode.core.model.user;

import com.icode.core.model.AbstractEntity;
import com.icode.core.model.house.UploadImage;
import com.icode.core.model.order.Order;
import org.hibernate.annotations.Type;
import org.joda.time.LocalDate;

import javax.persistence.*;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: xianfei
 * Date: 15-1-5
 * Time: 下午2:23
 * To change this template use File | Settings | File Templates.
 */
@Entity
@Table(name = "user_info")
public class UserInfo extends AbstractEntity {

    @OneToOne
    @JoinColumn(name = "user_id")
    private User user;
    @Column
    private String username;
    @Column
    private String realName;
    @Column(name = "gender")
    @Enumerated(EnumType.STRING)
    private Gender gender;
    @Column
    @Type(type = "org.jadira.usertype.dateandtime.joda.PersistentLocalDate")
    private LocalDate birthday;
    @Column
    private String IDCard;
    @Column
    private String email;
    @ManyToOne
    @JoinColumn(name = "province_id")
    private Province province;
    @ManyToOne
    @JoinColumn(name = "city_id")
    private City city;
    @ManyToOne
    @JoinColumn(name = "district_id")
    private District district;
    @Column
    private String area;
    @Column
    private String position;

    @OneToOne(cascade = CascadeType.ALL)
    @JoinColumn(name = "user_info_image_id")
    private UploadImage usersInfoImage;

    @OneToMany
    @JoinColumn(name = "user_info_id")
    private List<Order> orders;
    @Column(name = "exchange_num")
    private int exchangeNum = 0;

    public UserInfo() {
    }

    public UserInfo(User user) {
        this.user = user;
    }

    public UserInfo(String realName, User user, String IDCard, Province province, City city, District district, String area) {
        this.realName = realName;
        this.user = user;
        this.IDCard = IDCard;
        this.province = province;
        this.city = city;
        this.district = district;
        this.area = area;
    }

    public UserInfo(String username, String realName, Gender gender, LocalDate birthday, String IDCard, User user, String email, Province province, City city, District district, String area, String position) {
        this.username = username;
        this.realName = realName;
        this.gender = gender;
        this.birthday = birthday;
        this.IDCard = IDCard;
        this.user = user;
        this.email = email;
        this.province = province;
        this.city = city;
        this.district = district;
        this.area = area;
        this.position = position;
    }

    public void init(Province province, City city, District district, User user) {
        this.province = province;
        this.city = city;
        this.district = district;
        this.user = user;
    }

    public void initOrder(List<Order> orders) {
        this.orders = orders;
    }

    public void initUsersInfoImage(UploadImage uploadImage) {
        this.usersInfoImage = uploadImage;
    }

    public String username() {
        return username;
    }

    public String realName() {
        return realName;
    }

    public Gender gender() {
        return gender;
    }

    public LocalDate birthday() {
        return birthday;
    }

    public UploadImage usersInfoImage() {
        return usersInfoImage;
    }

    public String IDCard() {
        return IDCard;
    }

    public User user() {
        return user;
    }

    public String email() {
        return email;
    }

    public Province province() {
        return province;
    }

    public City city() {
        return city;
    }

    public District district() {
        return district;
    }

    public String area() {
        return area;
    }

    public String position() {
        return position;
    }

    public List<Order> orders() {
        return orders;
    }

    public int exchangeNum() {
        return exchangeNum;
    }

    public void setExchangeNum(int exchangeNum) {
        this.exchangeNum = exchangeNum;
    }

    public void update(String username, String realName, Gender gender, LocalDate birthday, String IDCard, String email, Province province, City city, District district, String area, String position) {
        this.username = username;
        this.realName = realName;
        this.gender = gender;
        this.birthday = birthday;
        this.IDCard = IDCard;
        this.email = email;
        this.province = province;
        this.city = city;
        this.district = district;
        this.area = area;
        this.position = position;

    }

    public void initUser(User user) {
        this.user = user;
    }
}
